package com.tzinfo.tzt.fragment;

import android.app.Activity;
import android.support.v4.app.FragmentManager;
import android.content.Context;
import android.net.Uri;
import android.os.AsyncTask;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v4.app.Fragment;
import android.support.v4.view.ViewPager;
import android.text.Editable;
import android.text.TextWatcher;
import android.view.KeyEvent;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.ListView;
import android.widget.RadioButton;
import android.widget.Spinner;
import android.widget.TextView;
import android.widget.Toast;

import com.google.gson.Gson;
import com.tzinfo.tzt.R;
import com.tzinfo.tzt.activity.util.FragmentHomeListviewAdapter;
import com.tzinfo.tzt.activity.util.FragmentHomeListviewAdapter2;
import com.tzinfo.tzt.activity.util.FragmentHomeListviewAdapter3;
import com.tzinfo.tzt.activity.util.HomeFragmentPageAdapter;
import com.tzinfo.tzt.util.HttpUtils;
import com.tzinfo.tzt.util.ResultMessage;

import org.apache.http.NameValuePair;
import org.apache.http.message.BasicNameValuePair;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;


public class HomeFragment extends Fragment implements View.OnClickListener{

    private OnFragmentInteractionListener mListener;

    private int bottombarCheckId =R.id.fragmengt_home_rb1;
    private List<HashMap<String, Object>> data;

    EditText editText1;

    private ViewPager mViewPager;
    private HomeFragmentPageAdapter mAdapter;

    private Context context;
    //   private  String url = "http://auto.tzinfo.net/mob/api/newsList.ashx";

    RadioButton radioButton1;
    RadioButton radioButton2;
    RadioButton radioButton3;



    public HomeFragment() {
        // Required empty public constructor
    }

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);

    }

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {
        // Inflate the layout for this fragment
        View view = inflater.inflate(R.layout.fragment_home, container, false);
        radioButton1 = (RadioButton) view.findViewById(R.id.fragmengt_home_rb1);
        radioButton2 = (RadioButton) view.findViewById(R.id.fragmengt_home_rb2);
        radioButton3 = (RadioButton) view.findViewById(R.id.fragmengt_home_rb3);
        radioButton1.setOnClickListener(this);
        radioButton2.setOnClickListener(this);
        radioButton3.setOnClickListener(this);

        mViewPager=(ViewPager)view.findViewById(R.id.fragmengt_home_viewPager);
        mViewPager.setOnPageChangeListener(new MyPagerOnPageChangeListener());

        return view;

    }

    @Override
    public void onActivityCreated(@Nullable Bundle savedInstanceState) {
        super.onActivityCreated(savedInstanceState);
        FragmentManager fragmentManager = getChildFragmentManager();
        mAdapter = new HomeFragmentPageAdapter(fragmentManager);
        mViewPager.setAdapter(mAdapter);

        setCurrentItem(bottombarCheckId);

    }


    // TODO: Rename method, update argument and hook method into UI event


    @Override
    public void onAttach(Activity activity) {
        super.onAttach(activity);
        context = activity;
    }

    @Override
    public void onDetach() {
        super.onDetach();
        mListener = null;
    }

    private class MyPagerOnPageChangeListener implements ViewPager.OnPageChangeListener {
        @Override
        public void onPageScrolled(int i, float v, int i2) {

        }

        @Override
        public void onPageSelected(int position) {
            if (position == 0) {
                radioButton1.performClick();
            }
            else if (position == 1) {
                radioButton2.performClick();
            }
            else if (position == 2) {
                radioButton3.performClick();
            }
        }

        @Override
        public void onPageScrollStateChanged(int i) {

        }
    }

    public void onClick(View v) {
        setCurrentItem(v.getId());
    }

    /**
     * This interface must be implemented by activities that contain this
     * fragment to allow an interaction in this fragment to be communicated
     * to the activity and potentially other fragments contained in that
     * activity.
     * <p>
     * See the Android Training lesson <a href=
     * "http://developer.android.com/training/basics/fragments/communicating.html"
     * >Communicating with Other Fragments</a> for more information.
     */
    public interface OnFragmentInteractionListener {
        // TODO: Update argument type and name
        public void onFragmentInteraction(Uri uri);
    }

    private void setCurrentItem(int checkId) {
        bottombarCheckId = checkId;
        switch (checkId) {
            case R.id.fragmengt_home_rb1:
                mViewPager.setCurrentItem(0);
                break;
            case R.id.fragmengt_home_rb2:
                mViewPager.setCurrentItem(1);
                break;
            case R.id.fragmengt_home_rb3:
                mViewPager.setCurrentItem(2);
                break;
        }
    }
}
